function Gn = slnrmgraph(G, ntype)
%SLNRMGRAPH Normalizes a graph
%
% [ Syntax ]
%   - Gn = slnrmgraph(G, 'stocha')
%
% [ Arguments ]
%   - G:        the original graph
%   - Gn:       the normalized graph
%
% [ Description ]
%   - Gn = slnrmgraph(G, 'stocha') normalizes the input graph into
%     a stochastic matrix, that is scale each row, so that the 
%     entries in each row sum to 1.
%
% [ History ]
%   - Created by Dahua Lin, on Jul 8, 2007
%

%% parse and verify input arguments

error(nargchk(2, 2, nargin));

assert(isnumeric(G) && ndims(G) == 2, 'sltoolbox:slnrmgraph:invalidarg', ...
    'The G should be a 2D numeric matrix.');
assert(ischar(ntype), 'sltoolbox:slnrmgraph:invalidarg', ...
    'The normalization type should be a string.');


%% main

switch ntype
    case 'stocha'
        rs = sum(G, 2);
        rs(rs == 0) = 1;
        Gn = bsxfun(@times, G, 1./rs);
        
    otherwise
        error('sltoolbox:slnrmgraph:unknowntype', ...
            'Unknown normalization type %s', ntype);
end
